package com.wangyadong.hobby.schedule.jobtask.map.hashtable;

import com.wangyadong.hobby.schedule.jobtask.map.AbstractMap;
import com.wangyadong.hobby.schedule.jobtask.tree.pq.Entry;

import java.util.ArrayList;
import java.util.Random;

/**
 * <p>
 *
 * @author <714037465@qq.com>
 * @since 2018/10/2419:26
 **/
public abstract class AbstractHashMap<K, V> extends AbstractMap {

    protected int n = 0;
    protected int capacity;
    private int prime;
    private long scale;
    private long shift;

    public AbstractHashMap(int capacity, int prime) {
        this.capacity = capacity;
        this.prime = prime;
        Random random = new Random();
        scale = random.nextInt(prime - 1) + 1;
        shift = random.nextInt(prime);
    }

    public AbstractHashMap(int capacity) {
        this(capacity, 10000);
    }

    public AbstractHashMap() {
        this(17, 10000);
    }


    @Override
    public Object get(Object k) {
        return buckGet(hashValue(k), k);
    }

    @Override
    public Object put(Object o, Object o2) {
        V v = bukcetPut(hashValue(o), o, o2);
        return v;
    }

    private void resize(int newCap){
        ArrayList<Entry> arrayList=new ArrayList<>(n);

    }

    protected abstract void createTable();

    protected abstract V buckGet(int h, Object k);

    protected abstract V bukcetPut(int h, Object k, Object v);

    protected abstract V remove(int h, K k);

    private int hashValue(Object key) {
        return (int) (Math.abs(key.hashCode() * scale + shift) % prime) % capacity;
    }
}
